<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<style>
		* {
			margin: 0;
			padding: 0;
		}
		.box {
			width: 200px;
			height: 200px;
			background-color: pink;
			position: absolute;
			left: 0;
			top: 0;
		}
	</style>
</head>
<body>
	<div class="box"></div>
	<script>
		var box = document.getElementsByClassName('box')[0];
		box.onmousedown = function () {
			var downEv = window.event || arguments[0];
			// 鼠标点下时候距离文档的X,Y方向的距离
			var downX = downEv.clientX;
			var downY = downEv.clientY;
			// 鼠标距离要移动的容器的距离
			var mouseToBoxLeft = downX - box.offsetLeft;
			var mouseToBoxTop = downY - box.offsetTop;
			document.onmousemove = function () {
				// 获取event对象，window.event是ie和chrome，arguments[0]是火狐
				var event = window.event || arguments[0];
				box.style.left = event.clientX - mouseToBoxLeft + 'px';
				box.style.top = event.clientY - mouseToBoxTop + 'px';
				// 鼠标松开取消鼠标移动事件
				document.onmouseup = function () {
					document.onmousemove = null;
				}
			}
		}
	</script>
</body>
</html>